package com.bxs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bxs.global.JsonResult;
import com.bxs.mapper.MailReceiverMapper;
import com.bxs.pojo.MailReceiver;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class MailReceiverController {
    @Autowired
    MailReceiverMapper mailReceiverMapper;

    @GetMapping("mailReceiverList")
    public JsonResult mailReceiverList(@RequestParam Map<String, Object> param){
        QueryWrapper<MailReceiver> mailReceiverQueryWrapper = new QueryWrapper<>();
        List<MailReceiver> mailReceivers = mailReceiverMapper.selectList(mailReceiverQueryWrapper);
        return JsonResult.success(mailReceivers);
    }

    @PostMapping("mailReceiverAdd")
    public JsonResult mailReceiverAdd(@RequestBody Map<String, Object> param) throws JsonProcessingException {
        param = (Map)param.get("data");
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(param);
        MailReceiver mailReceiver = objectMapper.readValue(s, MailReceiver.class);
        mailReceiverMapper.insert(mailReceiver);
        return JsonResult.success("success");
    }

    @GetMapping("mailReceiverDelete")
    public JsonResult mailReceiverDelete(@RequestParam Map<String, Object> param){
        Integer id = Integer.valueOf((String)param.get("id"));
        mailReceiverMapper.deleteById(id);
        return JsonResult.success("success");
    }

    @PostMapping("mailReceiverUpdate")
    public JsonResult mailReceiverUpdate(@RequestBody Map<String, Object> param) throws JsonProcessingException {
        param = (Map)param.get("data");
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(param);
        MailReceiver mailReceiver = objectMapper.readValue(s, MailReceiver.class);
        int cnt = mailReceiverMapper.updateById(mailReceiver);
        return JsonResult.success(cnt);
    }

}
